package Services

import(
	"github.com/gin-gonic/gin"
	"fmt"
	"net/http"
	"encoding/json"
	"gin/Databases"
	"gin/Common"
	"gin/Middlewares"
)

func quertMarketData() (bool, string) {
	var macros []Common.MacroDataTemp
	sql := "select Fid,Fdate,Ftype,Ftitle,Furl,Fsite from market_data_temp where Fid<20000000 limit 200;"
	err := Databases.Db.Select(&macros, sql)
	if err != nil {
		fmt.Println("exec failed, ", err)
		return false, ""
	}

	if (len(macros) == 0) {
		return true,"{}"
	}

    str, _ := json.Marshal(macros)
    str_res := string(str)
    return true, str_res
}

// 宏观数据
func MarketDataForCheck(c *gin.Context) {
	if (!Middlewares.CheckLogin(c)) {
		c.JSON(http.StatusOK, gin.H{
	        "result": -1,
	        "msg":    "login fail",
	        "data": "{}",
	    })
	    return
	}

	result,data := quertMarketData()
	if (!result) {
		c.JSON(http.StatusOK, gin.H{
	        "result": -2,
	        "msg":    "query fail",
	        "data": "{}",
	    })
	    return
	}

	
    //fmt.Println("str:", str)
    c.JSON(http.StatusOK, gin.H{
        "result": 0,
        "msg":    "success",
        "data": data,
    })
}

